package memento.fromCSDN.usingInnerClass;

/**
 * 原发器:象棋棋子类
 */
public class Chessman {
    private String label;
    private int x;
    private int y;

    public Chessman() {
    }

    public Chessman(String label) {
        this.label = label;
    }

    public Chessman(String label, int x, int y) {
        this.label = label;
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    //保存状态
    public ChessmanMemento save(){
        return new ChessmanMemento(this.label,this.getX(),this.getY());
    }

    //恢复状态
    public void restore(ChessmanMemento menento){
        this.label = menento.getLabel();
        this.x = menento.getX();
        this.y = menento.getY();
    }

    /**
     * 备忘录:内部类
     * 保证只有原生类才对其有操作权限，以保证备忘录的东西不会被其他类窜改
     */
    class ChessmanMemento {
        private String label;
        private int x;
        private int y;

        public ChessmanMemento(String label, int x, int y) {
            this.label = label;
            this.x = x;
            this.y = y;
        }

        public String getLabel() {
            return label;
        }

        public void setLabel(String label) {
            this.label = label;
        }

        public int getX() {
            return x;
        }

        public void setX(int x) {
            this.x = x;
        }

        public int getY() {
            return y;
        }

        public void setY(int y) {
            this.y = y;
        }
    }

}
